package edi.utils;

import edi.dao.*;
import edi.entities.*;
import edi.entities.enums.DocPermission;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import javax.annotation.PostConstruct;

@Service
public class Initializator {


	@Autowired
	public UserDao userDao;

	@Autowired
	public DepartmentDao departmentDao;

	@Autowired
	public DepartmentUserDao departmentUserDao;

	@Autowired
	public DocumentDao documentDao;

	@Autowired
	public DepUsrDocDao depUsrDocDao;

	@Autowired
	public DocTypeDao docTypeDao;

	public String MD5(String md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}

	@PostConstruct
	public void init() {
		UserEntity userEntity = new UserEntity();
		userEntity.setUsrLogin("test2");
		userEntity.setUsrPassword(MD5("test2"));
		userEntity.setUsrFirstName("Jan");
		userEntity.setUsrLastName("Kowalski");
		userEntity.setUsrStatus("1");
		userDao.saveOrUpdate(userEntity);


		UserEntity userEntity2 = new UserEntity();
		userEntity2.setUsrLogin("test1");
		userEntity2.setUsrPassword(MD5("test1"));
		userEntity2.setUsrFirstName("Bogdan");
		userEntity2.setUsrLastName("Nowak");
		userEntity2.setUsrStatus("1");
		userDao.saveOrUpdate(userEntity2);

		DepartmentEntity departmentEntity = new DepartmentEntity();
		departmentEntity.setDepCode("IT");
		departmentEntity.setDepName("IT Support");
		departmentDao.saveOrUpdate(departmentEntity);

		DepartmentEntity departmentEntity2 = new DepartmentEntity();
		departmentEntity2.setDepCode("CON");
		departmentEntity2.setDepName("Consulting");
		departmentDao.saveOrUpdate(departmentEntity2);

		DepartmentUserEntity departmentUserEntityWithoutDepartment = new DepartmentUserEntity();
		departmentUserEntityWithoutDepartment.setUdeDepartment(null);
		departmentUserEntityWithoutDepartment.setUdeUser(userEntity);
		departmentUserEntityWithoutDepartment.setCreatedBy(userEntity.getId());
		departmentUserEntityWithoutDepartment.setCreatedDate(new DateTime(System.currentTimeMillis()));
		departmentUserDao.saveOrUpdate(departmentUserEntityWithoutDepartment);

		DepartmentUserEntity departmentUserEntityWithoutDepartment2 = new DepartmentUserEntity();
		departmentUserEntityWithoutDepartment2.setUdeDepartment(null);
		departmentUserEntityWithoutDepartment2.setUdeUser(userEntity2);
		departmentUserEntityWithoutDepartment2.setCreatedBy(userEntity2.getId());
		departmentUserEntityWithoutDepartment2.setCreatedDate(new DateTime(System.currentTimeMillis()));
		departmentUserDao.saveOrUpdate(departmentUserEntityWithoutDepartment2);

		DepartmentUserEntity departmentUserEntity = new DepartmentUserEntity();
		departmentUserEntity.setUdeDepartment(departmentEntity);
		departmentUserEntity.setUdeUser(userEntity);
		departmentUserEntity.setCreatedBy(userEntity.getId());
		departmentUserEntity.setCreatedDate(new DateTime(System.currentTimeMillis()));
		departmentUserDao.saveOrUpdate(departmentUserEntity);

		DepartmentUserEntity departmentUserEntity2 = new DepartmentUserEntity();
		departmentUserEntity2.setUdeDepartment(departmentEntity2);
		departmentUserEntity2.setUdeUser(userEntity2);
		departmentUserEntity2.setCreatedBy(userEntity2.getId());
		departmentUserEntity2.setCreatedDate(new DateTime(System.currentTimeMillis()));
		departmentUserDao.saveOrUpdate(departmentUserEntity2);

		DocTypeEntity docTypeEntity = new DocTypeEntity();
		docTypeEntity.setDctExtension("txt");
		docTypeEntity.setDctIcon(new byte[1]);
		docTypeEntity.setDctName("Text file");
		docTypeDao.saveOrUpdate(docTypeEntity);

//		DocumentEntity documentEntity = new DocumentEntity();
//		documentEntity.setDocName(DOCUMENT_ONE);
//		documentEntity.setDocDctId(docTypeEntity);
//		documentEntity.setIsFinal("0");
//		documentDao.saveOrUpdate(documentEntity);

//		DepUsrDocEntity depUsrDocEntity = new DepUsrDocEntity();
//		depUsrDocEntity.setDudDepartmentUser(departmentUserEntity2);
//		depUsrDocEntity.setDudDocument(documentEntity);
//		depUsrDocEntity.setDudPermission(DocPermission.OWNER);
//		depUsrDocEntity.setCreatedBy(userEntity.getId());
//		depUsrDocEntity.setCreatedDate(new DateTime(System.currentTimeMillis()));
//		depUsrDocDao.save(depUsrDocEntity);
	}
}
